package com.example.demo.service.impl;

import com.example.demo.beans.Result;
import com.example.demo.beans.entity.ImmeEmp;
import com.example.demo.beans.entity.ImmeUser;
import com.example.demo.beans.entity.ImmeUserExample;
import com.example.demo.dao.mapper.ImmeEmpMapper;
import com.example.demo.dao.mapper.ImmeUserMapper;
import com.example.demo.service.EmpService;
import com.example.demo.util.AESUtil;
import com.example.demo.util.CommUtil;
import com.example.demo.util.Constant;
import com.example.demo.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    ImmeEmpMapper empMapper;

    @Autowired
    ImmeUserMapper userMapper;

    @Autowired
    HttpServletResponse response;

    @Override
    public List<ImmeEmp> getAllEmp() {
        return empMapper.getAll();
    }

    @Override
    public Result login(ImmeUser user) {
        ImmeUserExample example = new ImmeUserExample();
        ImmeUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andPasswordEqualTo(user.getPassword());

        List<ImmeUser> users = userMapper.selectByExample(example);
        if(CommUtil.isListNotBlank(users)){
            ImmeUser user1 = users.get(0);

            String loginInfo = user1.getId() + ":" + new Date().getTime();
            log.info("生成登录loginInfo：{}",loginInfo);

            String token = AESUtil.encryptToString(loginInfo,Constant.TOKEN_KEY);
            log.info("生成登录token：{}",token);

            Map data = new HashMap();
            data.put("token",token);

            Cookie cookie = new Cookie("token",token);
            cookie.setHttpOnly(true);
            //cookie.setMaxAge(Constant.SESSION_TIMEOUT);
            response.addCookie(cookie);

            return new Result(true,data);
        }
        else {
            return new Result(false,"用户名或密码错误");
        }
    }

    @Override
    public Result checkLogin(HttpServletRequest request) {
        try{
            if(request == null){
                return new Result(false,"请求为空",Constant.LOGIN_CODE_TIMEOUT);
            }

            String token = request.getParameter("token");
            log.info("参数带来的token:{}",token);
            if(StringUtil.isBlank(token)){
                return new Result(false,"登录信息token为空",Constant.LOGIN_CODE_TIMEOUT);
            }

            String tokenInfo = AESUtil.decryptToString(token,Constant.TOKEN_KEY);
            log.info("解密后的tokenInfo：{}",tokenInfo);
            String[] tokenInfoArr = tokenInfo.split(":");

            String userId = tokenInfoArr[0];
            String timeStamp = tokenInfoArr[1];

            ImmeUser user = userMapper.selectByPrimaryKey(userId);
            if(user == null){
                return new Result(false,"用户不存在",Constant.LOGIN_CODE_TIMEOUT);
            }

            Date loginDate = new Date(Long.parseLong(timeStamp));

            long sysDateL = new Date().getTime();
            Long min = ((sysDateL - loginDate.getTime())%86400000%3600000)/60000;

            if(min > Constant.LOGIN_TIMEOUT){
                return new Result(false,"登录超时！",Constant.LOGIN_CODE_TIMEOUT);
            }

            return new Result(true);
        }catch (Exception e){
            log.error("",e);
        }

        return new Result(false,"判断登录状态出错",Constant.LOGIN_CODE_TIMEOUT);
    }
}
